package com.southfield.servlet;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;


@SuppressWarnings("serial")
public class HttpUtilServlet extends HttpServlet {
	
	private static final int BYTES_DOWNLOAD = 1024;

    public void doGet(HttpServletRequest request, 
    		HttpServletResponse response) throws IOException
    {
    	ServletContext application = getServletContext();
    	
    	String reqType = request.getParameter("reqType");
    	
    	System.out.println("HttpImageServlet :: doGet :: reqType is : " + reqType);
    	
    	if(reqType.equalsIgnoreCase("1")) {
    		System.out.println("HttpUtilServlet :: doGet :: reqType is : " + reqType + " for Image");

        	// Get the path to image we want to return
            String filepath = application.getRealPath("WEB-INF/images/Rotating_earth.gif");
        
            // Get image MIME type
            String mimeType = application.getMimeType(filepath);
            if (mimeType == null) {
                application.log("Could not get MIME type of "+filepath);
                response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                return;
            }
        
            // Set content type
            response.setContentType(mimeType);
        
            // Set content size
            File file = new File(filepath);
            response.setContentLength((int)file.length());
            
            // Copy the contents of the file to the responses output stream
            FileInputStream in = new FileInputStream(file);
            OutputStream out = response.getOutputStream();
            byte[] buf = new byte[1024];
            int len = 0;
            while ((len = in.read(buf)) >= 0) {
                out.write(buf, 0, len);
            }
            
            in.close();
            out.close();
            
        } else if(reqType.equalsIgnoreCase("2")) {
    		
    		System.out.println("HttpUtilServlet :: doGet :: reqType is : " + reqType + " for text file");
    		
    		response.setContentType("text/plain");
    		response.setHeader("Content-Disposition",
    	                     "attachment;filename=downloadname.txt");
    		ServletContext ctx = getServletContext();
            String filepath = ctx.getRealPath("WEB-INF/images/testing.txt");

            File file = new File(filepath);
            FileInputStream in = new FileInputStream(file);


    		int read=0;
    		byte[] bytes = new byte[BYTES_DOWNLOAD];
    		OutputStream os = response.getOutputStream();
    	 
    		while((read = in.read(bytes))!= -1){
    			os.write(bytes, 0, read);
    		}
    		
    		in.close();
    		os.flush();
    		os.close();	
    		

    	} else if(reqType.equalsIgnoreCase("3")) {
    		
    		System.out.println("HttpUtilServlet :: doGet :: reqType is : " + reqType + " for pdf file");

    		
    		ServletContext ctx = getServletContext();
    		String filepath = ctx.getRealPath("WEB-INF/images/sample-pdf.pdf");

            File file = new File(filepath);
            FileInputStream in = new FileInputStream(file);


    		response.setContentType("application/pdf");
    		response.addHeader("Content-Disposition", "attachment; filename=" + file);
    		response.setContentLength((int) file.length());

    		OutputStream os = response.getOutputStream();
    		int bytes;
    		while ((bytes = in.read()) != -1) {
    			os.write(bytes);
    		}
    			
    		in.close();
    		os.flush();
    		os.close();	

    	}else if(reqType.equalsIgnoreCase("4")) {
    		
    		System.out.println("HttpUtilServlet :: doGet :: reqType is : " + reqType + " for JSON from a file");

    		
    		ServletContext ctx = getServletContext();
    		String filepath = ctx.getRealPath("WEB-INF/images/jsontesting.txt");

            File file = new File(filepath);
            FileInputStream in = new FileInputStream(file);


    		response.setContentType("application/json");
    		//response.addHeader("Content-Disposition", "attachment; filename=" + file);
    		response.setContentLength((int) file.length());

    		OutputStream os = response.getOutputStream();
    		int bytes;
    		while ((bytes = in.read()) != -1) {
    			os.write(bytes);
    		}
    			
    		in.close();
    		os.flush();
    		os.close();

    	}else if(reqType.equalsIgnoreCase("5")) {
    		
    		System.out.println("HttpUtilServlet :: doGet :: reqType is : " + reqType + " for JSON");

    		response.setContentType("application/json");
    		JSONArray arrayObj=new JSONArray();
    		 arrayObj.add("MCA");
    		 arrayObj.add("Amit Kumar");
    		 arrayObj.add("19-12-1986");
    		 arrayObj.add(24);
    		 arrayObj.add("Scored");
    		 arrayObj.add(new Double(66.67));
    		 PrintWriter out = response.getWriter();
    		 out.println(arrayObj);
    		 for(int i=0;i<arrayObj.size();i++) {
    			 out.println(arrayObj.getString(i));
    		  }

    	}

    }
    	
}


